package com.eastdigit.data;

/**
 * Created by acer on 14-2-9.
 */

import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class DBUtil {

    public static DataTable getTableInfo(Connection conn, boolean closeFlag) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
            ResultSet rs = dbm.getTables(currentCatalog, null, null, null);
            return new DataTable(rs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    
    public static boolean findTable(Connection conn, String tableName) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
            String[] types   =   {"TABLE"};  
            ResultSet tabs = dbm.getTables(currentCatalog, null, tableName, types);  
            if (tabs.next()) {  
                return true;  
            }  
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return false;
    }

    public static DataTable getColumnInfo(Connection conn, String tableName, boolean closeFlag) {
        try {
        	DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
            ResultSet rs = dbm.getColumns(currentCatalog, null, tableName, null);
            
            DataTable dt = new DataTable(rs);

            rs = dbm.getPrimaryKeys(currentCatalog, null, tableName);
            DataTable keyDt = new DataTable(rs);
            Map map = keyDt.toMap("Column_Name", "PK_Name");
            dt.insertColumn("isKey");
            for (int i = 0; i < dt.getRowCount(); i++) {
                DataRow dr = dt.getDataRow(i);
                if (map.containsKey(dr.getString("Column_Name"))) {
                    dr.set("isKey", "Y");
                } else {
                    dr.set("isKey", "N");
                }
            }
            return dt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static DataTable getSQLTypes(Connection conn, boolean closeFlag) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            ResultSet rs = dbm.getTypeInfo();
            DataTable dt = new DataTable(rs);
            return dt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static String getDbType(JdbcTemplate jdbcTemplate) {
        String DBType = null;
        Connection conn = null;
        try {
            conn = jdbcTemplate.getDataSource().getConnection();
            DatabaseMetaData md = conn.getMetaData();
            DBType = md.getDatabaseProductName().toUpperCase();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return DBType;
    }

    public static String columnNameParser(String name) {
        if (name.startsWith("C_")) {// 去的code表C_开头
            name = name.substring(2);
        }
        String newNames = "";
        if (name.indexOf("_") > 0) {
            // 驼峰法则
            String[] names = name.toLowerCase().split("_");
            newNames = names[0];
            for (int j = 1; j < names.length; j++) {
                newNames += names[j].substring(0, 1).toUpperCase() + names[j].substring(1);
            }
        } else if (name.equals(name.toUpperCase())) {
            newNames = name.toLowerCase();
        } else {
            newNames = name;
        }
        return newNames;
    }
}
